package com.woniuxy.goods.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.goods.module.goods.Goods;
import com.woniuxy.goods.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Slf4j
@RefreshScope
public class GoodsController {
    @Autowired
    GoodsService goodsService ;

    @Value("${server.port}")
    int port ;

    @Value("${flag}")
    String flag ;

    @Value("${message}")
    String message ;

    @GetMapping("/goods/listPage")
    public PageInfo<Goods> findGoodsByPage(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "2")Integer size,
            @RequestParam(value = "name",defaultValue = "") String name ,
            @RequestParam(value = "tid",defaultValue = "0")Integer tid){

        return goodsService.findGoodsByPage(now,size,name,tid);
    }

    @PostMapping("/goods/add")
    public Integer add(@RequestBody Goods goods){
        //推荐使用
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        return goodsService.add(goods);
    }


    @GetMapping("/goods/ok")
    public String ok(){

        log.info("当前请求的服务器端口是----->"+this.port);

        return "蜗牛学院!!!-----";
    }

    @GetMapping("/goods/findGoodsByKey")
    public Goods findGoodsByKey(int id) {
        log.info("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA======================>"+this.message);
        //int i = 10 / 0 ;
        return goodsService.findGoodsByKey(id);
    }
}
